##########################################################################################

library(optparse)
library(ArchR)

##########################################################################################

option_list <- list(
    make_option(c("--comine_data_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    ## 单细胞的atac
    comine_data_file <- "~/20231121_singleMuti/results/qc_atac_v3/germ/testis_combined_peak.combineRNA.qc.Rdata"

    out_path <- "~/20231121_singleMuti/results/celltype_plot/mfuzz"
}

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

comine_data_file <- opt$comine_data_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

##########################################################################################

b <- load(comine_data_file)

##########################################################################################
## archR自带算法计算表达和motif的相关系数
corGIM_MM <- correlateMatrices(
    ArchRProj = testis_combined_peak_combineRNA,
    useMatrix1 = "GeneExpressionMatrix",
    useMatrix2 = "MotifMatrix" 
)

corGIM_MM <- corGIM_MM[corGIM_MM$MotifMatrix_matchName == corGIM_MM$GeneExpressionMatrix_name,]
corGIM_MM <- corGIM_MM[!is.na(corGIM_MM$cor),]

## 输出
out_file <- paste0( out_path , "/cor.motif_atac-rna.tsv" )
write.table( corGIM_MM , out_file , row.names = F , sep = "\t" )